package com.ruoyi.kjy.controller;/**
 * @InterfaceName: AnnouncementController
 * @Description:
 * @Author: YourName
 * @Date: 2023-04-12
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.kjy.domain.Announcement;
import com.ruoyi.kjy.service.IAnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 *@InterfaceName: AnnouncementController
 *@Description:
 *@Author: lilirui
 *@Date: 2023-04-12
 */
@Api(tags = "公告")
@RestController
@RequestMapping("/announcement")
public class AnnouncementController extends BaseController {
    @Autowired
    private IAnnouncementService IAnnouncementService;

    /**
     * 查询科技成果管理列表a
     */
    @GetMapping("/list")
    public AjaxResult list(Long current,Long size, Announcement announcement) {
        LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(announcement);
        if (Objects.isNull(current)){
            current=1l;
        }
        if (Objects.isNull(size)){
            size=10l;
        }
        if (size>=20l){
            size= 20l;
        }
        queryWrapper.orderByDesc(Announcement::getCreateTime);
        Page<Announcement> page = IAnnouncementService.page(new Page<>(current, size), queryWrapper);
//        List<Announcement> records = page.getRecords();
//        for (Announcement record : records) {
//            record.setContent(null);
//        }
        return AjaxResult.success(page);
    }


    @GetMapping("/listAnnouncement")
    public AjaxResult listAnnouncement(Long current,Long size, Announcement announcement) {
        LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();
        announcement.setStatus(1);
        queryWrapper.setEntity(announcement);
        if (Objects.isNull(current)){
            current=1l;
        }
        if (Objects.isNull(size)){
            size=10l;
        }
        if (size>=20l){
            size= 20l;
        }
        queryWrapper.orderByDesc(Announcement::getCreateTime);
        Page<Announcement> page = IAnnouncementService.page(new Page<>(current, size), queryWrapper);
//        List<Announcement> records = page.getRecords();
//        for (Announcement record : records) {
//            record.setContent(null);
//        }
        return AjaxResult.success(page);
    }

    /**
     * 导出科技成果管理列表
     */
    @Log(title = "科技成果管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Announcement announcement) {
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(announcement);
        List<Announcement> list = IAnnouncementService.list(queryWrapper);
        ExcelUtil<Announcement> util = new ExcelUtil<Announcement>(Announcement.class);
        util.exportExcel(response, list, "科技成果管理数据");
    }

    /**
     * 获取科技成果管理详细信息
     */
    @ApiOperation("详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(IAnnouncementService.getById(id));
    }

    /**
     * 新增科技成果管理
     */
    @ApiOperation("发布")
    @Log(title = "科技成果管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Announcement announcement) {
        return toAjax(IAnnouncementService.save(announcement));
    }

    /**
     * 修改科技成果管理
     */
    @Log(title = "科技成果管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Announcement announcement) {
        return toAjax(IAnnouncementService.updateById(announcement));
    }


    @Log(title = "科技成果管理", businessType = BusinessType.UPDATE)
    @PutMapping("/reviewer")
    public AjaxResult reviewer(@RequestBody Announcement announcement) {
        announcement.setReviewer(SecurityUtils.getUsername());
        return toAjax(IAnnouncementService.updateById(announcement));
    }

    /**
     * 删除科技成果管理
     */
    @Log(title = "科技成果管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(IAnnouncementService.removeByIds(Arrays.asList(ids)));
    }
}
